﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DOTNET_ASSIGNMENT.Models;

namespace DOTNET_ASSIGNMENT.Services
{
    public class FileServices
    {
        public bool AddFile(File file)
        {
            using (DataContext db = new DataContext())
            {
                file.Date = DateTime.Now.ToShortDateString();
                file.IsBlocked = false;
                db.Files.Add(file);
                db.SaveChanges();
            }
            return true;
        }

        public File GetFile(string email, string fileName)
        {
            using (DataContext db = new DataContext())
            {
                var items = db.Files.Where(x => x.CustomerEmail == email);
                var item = items.First(x => x.DisplayName == fileName);
                return item;
            }
        }

        public  IEnumerable<File> GetFiles(string email)
        {
            using (DataContext db = new DataContext())
            {
                var items = db.Files.Where(x => x.CustomerEmail == email && x.IsBlocked == false).ToList();
                return items;
            }
        }

        public IEnumerable<File> GetAllFiles()
        {
            using (DataContext db = new DataContext())
            {
                var items = db.Files.ToList();
                return items;
            }
        }

        public bool DeleteFile(string email, string fileName)
        {
            bool deleteSuccess = false;
            using (DataContext db = new DataContext())
            {
                try
                {
                    var items = db.Files.Where(x => x.CustomerEmail == email);
                    var item = items.First(x => x.DisplayName == fileName);
                    if (item != null)
                    {
                        db.Files.Remove(item);
                        db.SaveChanges();
                        deleteSuccess = true;
                    }
                }
                catch
                {
                    return false;
                }
                return deleteSuccess;
            }
        }

       public bool IsSameName(string email, string name)
        {
           using (DataContext db = new DataContext())
           {
               var items = db.Files.Where(x => x.CustomerEmail == email).ToList();

               foreach(var item in items)
               {
                   if (item.DisplayName == name)
                   {
                       return true;
                   }
               }
               return false;
           }
        }

        public string ChangeFileName(string email, string name)
        {
            int count = 0;
            string nameOrgin = name;
            while (IsSameName(email, name))
            {
                name = nameOrgin;
                name = name + "(" + count.ToString() + ")";
                ++count;
            }
            return name;
       }

        public File GetFileFromUrl(string url)
        {
            using (DataContext db = new DataContext())
            {
                var file = db.Files.Single(x => x.URL == url && x.IsBlocked == false);

                return file;
            }
        }

        public bool BlockFile(string filename)
        {
            using (DataContext db = new DataContext())
            {
                var file = db.Files.Single(x => x.DisplayName == filename);

                if (file == null) return false;

                file.IsBlocked = true;
                db.SaveChanges();
                return true;
            }
        }

        public bool UnBlockFile(string filename)
        {
            using (DataContext db = new DataContext())
            {
                var file = db.Files.Single(x => x.DisplayName == filename);

                if (file == null) return false;

                file.IsBlocked = false;
                db.SaveChanges();
                return true;
            }
        }

    }
}